 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>02计算器</title>
</head>
<body>
<input type="number" placeholder="请输入数字1" id="num1">
<br>
<input type="number" placeholder="请输入数字2" id="num2">
<br>
<button onclick="calc('+')">+</button>
<button onclick="calc('-')">-</button>
<button onclick="calc('*')">*</button>
<button onclick="calc('/')">/</button>
<h4>运算结果:<span></span></h4>
<script>
    function calc(o) {
        let num1 = document.getElementById('num1').value;
        let num2 = document.getElementById('num2').value;
        let result = document.querySelector('span');
        switch (o) {
            case '+':
                result.innerHTML = Number(num1) + Number(num2);
                break;
            case '-':
                result.innerHTML = Number(num1) - Number(num2);
                break;
            case '*':
                result.innerHTML = Number(num1) * Number(num2);
                break;
            case '/':
                if (num2 == 0) {
                    alert('除数不能为0');
                    return;
                }
                else {
                    result.innerHTML = Number(num1) / Number(num2);
                    break;
                }
        }
}
</script>
</body>
</html>